// 百度地图相关接口
const AKweather = 'dyigaKsSXxx1fWV1pPOGBYSBZiYmosGX'
const ak = '6dKoi5fFnOdYO1DnrZhytcwhPCUizujh' // 'qpj0z20DHYAnbcwbZKN4IP2Xhn1ec5zo'
const serverAK='hQQFqrueHxMPi9U1QFTzNc3wUKWFv18k' // by zywu
const serviceId = '218308'// '152868'
const baseUrl = 'http://yingyan.baidu.com/api/v3/'

// 逆地址解析
const reverse_geocoding=(pos,cb)=>{
    $.ajax({
        // url: '/baiduapi/reverse_geocoding/v3/?location='+(pos.lat + ',' + pos.lon)+'&output=json&ak=' + serverAK,
        url: 'http://api.map.baidu.com/reverse_geocoding/v3/?location='+(pos.lat + ',' + pos.lon)+'&output=json&ak=' + serverAK,
        dataType: 'jsonp',
        success: function (res) {
            if (res.status === 0) {
                cb(res.result.addressComponent)
            }
        }
    })
}

// 获取天气
export const getWeather = (data, cb) => {
    reverse_geocoding(data,(addr)=>{
        $.ajax({
            // 此服务jsonp方式请求无效
            url: '/baiduapi/weather/v1/?data_type=all&district_id='+addr.adcode+'&output=json&ak=' + serverAK,
            dataType: 'json',
            success: function (res) {
                if (res.status === 0) {
                    cb(res)
                } else {
                    // window.topVue.$Ctl.message('获取天气失败')
                }
            },
            error: function () {
                // window.topVue.$Ctl.message('获取天气失败')
            }
        })
    })
}

// 获取车辆实时位置
export const getCurrentPosition = (carId, cb, errfn) => {
    $.ajax({
        url: baseUrl + 'track/getlatestpoint' + '?ak=' + ak + '&service_id=' + serviceId + '&entity_name=' + carId,
        // baseUrl + 'entity/search' + '?ak=' + ak + '&page_index=1&page_size=1&service_id=' + serviceId + '&query=' + carId,
        dataType: 'jsonp',
        cache: false,
        success: function (res) {
            cb(res)
        },
        error: function (error) {
            if (errfn) {
                errfn(error)
            } else {
                window.topVue.$Ctl.message(carId + '获取车辆定位信息失败！')
            }
        }
    })
}

// 获取车辆历史位置
export const getHistoryDao = (carId, startTime, endTime, cb, errfn) => {
    $.ajax({
        url: baseUrl + 'track/gettrack' + '?ak=' + ak + '&service_id=' + serviceId + '&entity_name=' + carId + '&start_time=' + startTime + '&end_time=' + endTime + '&page_index=1&page_size=5000',
        dataType: 'jsonp',
        cache: false,
        success: function (res) {
            cb(res)
        },
        error: function (error) {
            if (errfn) {
                errfn(error)
            } else {
                window.topVue.$Ctl.message(carId + '获取车辆历史位置数据失败！')
            }
        }
    })
}
